package com.ebt.m.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.R;

/**
 * 对话框基类
 *
 * @author Michael.li
 */
public abstract class BaseAlertDialog implements DialogInterface.OnClickListener {
    protected AlertDialog.Builder builder;
    protected OnDialogListener mListener;

    public BaseAlertDialog(Context context) {

//		TextView title = new TextView(context);
//		title.setText(getTitle());
//		title.setGravity(Gravity.CENTER);
//		title.setTextSize(18);
//		title.setTextColor(getTitleColor());
//		title.setHeight(UIHelper.dip2px(context, 60));

        View titleView = LayoutInflater.from(context).inflate(
                R.layout.widget_title_alert_dialog, null);
        TextView tv_title = (TextView) titleView.findViewById(R.id.tv_title);
        tv_title.setText(getTitle());
        builder = new AlertDialog.Builder(context);
        builder.setCustomTitle(titleView);

    }

    public BaseAlertDialog(Context context, String titleStr) {
        View titleView = LayoutInflater.from(context).inflate(
                R.layout.widget_title_alert_dialog, null);
        TextView tv_title = (TextView) titleView.findViewById(R.id.tv_title);
        tv_title.setText(titleStr);
        builder = new AlertDialog.Builder(context);
        builder.setCustomTitle(titleView);

    }

    public BaseAlertDialog(Context context, int theme) {

        View titleView = LayoutInflater.from(context).inflate(
                R.layout.widget_title_alert_dialog, null);
        TextView tv_title = (TextView) titleView.findViewById(R.id.tv_title);
        tv_title.setText(getTitle());
        //TODO
//		ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context, R.style.AppTheme);
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context, theme);

        builder = new AlertDialog.Builder(new ContextThemeWrapper(context, theme));
        builder.setCustomTitle(titleView);
    }

    /**
     * 获取标题
     *
     * @return
     */
    protected abstract String getTitle();

    /**
     * 获取标题颜色
     *
     * @return
     */
    protected int getTitleColor() {
        return Color.parseColor("#0098ff");
    }

    ;

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                if (mListener != null) {
                    mListener.onConfirmed();
                }
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                if (mListener != null) {
                }
                break;
            default:
                break;
        }
    }

    public Dialog show() {
        return builder.show();
    }

    public interface OnDialogListener {
        void onConfirmed(int intValue);

        void onConfirmed();
    }

    public void setListener(OnDialogListener l) {
        this.mListener = l;
    }

    public OnDialogListener getListener() {
        return this.mListener;
    }
}
